import unittest
from selenium import webdriver
from time import sleep

from selenium.common.exceptions import NoAlertPresentException, NoSuchElementException


class MyBase(unittest.TestCase):
    def setUp(self) -> None:
        self.driver = webdriver.Chrome(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
        self.driver.implicitly_wait(5)

    def tearDown(self) -> None:
        sleep(2)
        self.driver.quit()

    def is_alert_present(self):
        try:
            self.driver.switch_to.alert
        except NoAlertPresentException:
            return False
        return True

    def is_element_present(self,how,what):
        try:
            self.driver.find_element(how,what)
        except NoSuchElementException:
            return False
        return True

    def close_alert_and_get_its_text(self,is_accept=True):
        a=self.driver.switch_to.alert
        t=a.text
        if is_accept:
            a.accept()
        else:
            a.dismiss()
        return t